﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZSZ.DTO;
using ZSZ.Iservice;
using ZSZ.Service.Entity;

namespace ZSZ.Service
{
    public class HouseAppointmentService : IHouseAppointmentService
    {
        public long AddNew(long? userId, string name, string phoneNum, long houseId, DateTime visitDate)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                HouseAppointmentEntity houseAppointment = new HouseAppointmentEntity();
                houseAppointment.UserId = userId;
                houseAppointment.Name = name;
                houseAppointment.PhoneNum = phoneNum;
                houseAppointment.HouseId = houseId;
                houseAppointment.VisitDate = visitDate;
                houseAppointment.Status = "未处理";
                ct.HouseAppointments.Add(houseAppointment);
                ct.SaveChanges();
                return houseAppointment.Id;
            }
        }

        public bool Follow(long adminUserId, long houseAppointmentId)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<HouseAppointmentEntity> cs = new CommonService<HouseAppointmentEntity>(ct);
                var house = cs.GetById(houseAppointmentId);
                if (house == null)
                {
                    throw new ArgumentException("没有这个预约");
                }
                if (house.FollowAdminUserId!=null)
                {
                    if (house.FollowAdminUserId == adminUserId)
                    {
                        return true;
                    }
                    return false;
                }
                try
                {
                    house.FollowAdminUserId = adminUserId;
                    ct.SaveChanges();
                }
                catch (Exception)
                {
                    return false;
                }
                return true;
            }
        }
        private HouseAppointmentDTO ToDTO(HouseAppointmentEntity e)
        {
            HouseAppointmentDTO dto = new HouseAppointmentDTO();
            dto.CommunityName = e.House.Community.Name;
            dto.CreateTime = e.CreateDatatime;
            dto.FollowAdminUserId = e.FollowAdminUserId;
            dto.FollowAdminUserName = e.AdminUser.Name;
            dto.FollowDateTime = e.FollowDateTime;
            dto.HouseId = e.HouseId;
            dto.Id = e.Id;
            dto.Name = e.Name;
            dto.PhoneNum = e.PhoneNum;
            dto.RegionName = e.House.Community.Region.Name;
            dto.Status = e.Status;
            dto.UserId = e.UserId;
            dto.VisitDate = e.VisitDate;
            return dto;
        }

        public HouseAppointmentDTO GetById(long id)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<HouseAppointmentEntity> cs = new CommonService<HouseAppointmentEntity>(ct);
                if (cs.GetById(id) == null)
                {
                    throw new ArgumentException("没有这个预约");
                }
                return ToDTO(cs.GetAll().Include(h => h.AdminUser).Include(h => h.House.Community).Include(h => h.House).Include(h => h.House.Community.Region).AsNoTracking().SingleOrDefault(h => h.Id == id));
            }
        }

        public HouseAppointmentDTO[] GetPagedData(long cityId, string status, int pageSize, int currentIndex)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<HouseAppointmentEntity> cs = new CommonService<HouseAppointmentEntity>(ct);
                return cs.GetAll().Include(h => h.AdminUser).Include(h => h.House).Include(h => h.House.Community).Include(h => h.House.Community.Region)
                    .AsNoTracking().Where(h => h.House.Community.Region.CityId == cityId && h.Status == status).OrderByDescending(h => h.CreateDatatime).Skip(currentIndex)
                    .Take(pageSize).Select(h=>ToDTO(h)).ToArray();
            }
        }

        public long GetToTalCount(long cityId, string status)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<HouseAppointmentEntity> cs = new CommonService<HouseAppointmentEntity>(ct);
                return cs.GetAll().LongCount(h=>h.House.Community.Region.CityId==cityId&&h.Status==status);
            }
        }
    }
}
